#!/bin/bash
#
#
# ``plugin.sh`` calls the following methods in the sourced driver:
#
# - nb_db_driver_install_server
# - nb_db_driver_install_client
# - nb_db_driver_start_server
# - nb_db_driver_stop_server
# - nb_db_driver_clean
# - nb_db_driver_configure

REDIS_VERSION=3.0.6

function _redis_env {
    # REMOTE_DB_* initialized after sourcing
    export REDIS_SERVER_LIST=$(echo $REMOTE_DB_HOSTS | sed 's/,/ /g')
    export REMOTE_PORT_START=$(echo $REDIS_SERVER_LIST | awk '{print $1}' | cut -d: -f2)
    export REDIS_SERVER_IPS=$(echo $REDIS_SERVER_LIST | awk -F: 'BEGIN {RS=" "} { print $1 }' | sort | uniq)
    export NODE_COUNT_END=5
    export REMOTE_PORT_END=`expr $REMOTE_PORT_START + $NODE_COUNT_END`
    export REDIS_PORT=`seq $REMOTE_PORT_START $REMOTE_PORT_END`
}

function _configure_redis {
    _redis_env
    pushd /opt/redis3/conf
    sudo sh -c "grep -q ulimit /etc/profile ||
                echo ulimit -SHn 40960 >> /etc/profile"
    sudo sh -c "echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse"
    sudo sh -c "echo never > /sys/kernel/mm/transparent_hugepage/enabled"
    sudo sh -c "echo 2048 > /proc/sys/net/core/somaxconn"
    sudo sh -c "grep -q vm.overcommit_memory /etc/sysctl.conf ||
                echo vm.overcommit_memory = 1 >> /etc/sysctl.conf"
    sudo sh -c "sysctl -p"
    for redisserver in $REDIS_SERVER_IPS; do
        for port in $REDIS_PORT; do
            echo "shutdown redis: "$redisserver:$port
            cp redis.conf /opt/redis3/conf/redis-$port.conf
            sed -i "s/6379/$port/g" redis-$port.conf
            sed -i "s/daemonize no/daemonize yes/g" redis-$port.conf
            sed -i "s/dump.rdb/dump-$port.rdb/g" redis-$port.conf
            sed -i "s/# cluster-enabled yes/cluster-enabled yes/g" redis-$port.conf
            sed -i "s/# cluster-config-file/cluster-config-file/g" redis-$port.conf
            sed -i "s/pubsub 32mb 8mb 60/pubsub 0 0 0/g" redis-$port.conf
        done
    done
    popd
}

function nb_db_driver_install_server {
    if [ "$DF_REDIS_INSTALL_FROM_RUBY" == "True" ]; then
        echo "Installing Redis cluster"
        if [ ! -f "$DEST/redis/redis-$REDIS_VERSION/redis" ]; then
            mkdir -p $DEST/redis
            if [ ! -f "$DEST/redis/redis-$REDIS_VERSION.tar.gz" ]; then
                wget http://download.redis.io/releases/redis-$REDIS_VERSION.tar.gz -O $DEST/redis/redis-$REDIS_VERSION.tar.gz
            fi
            tar xzvf $DEST/redis/redis-$REDIS_VERSION.tar.gz -C $DEST/redis
            pushd $DEST/redis/redis-$REDIS_VERSION
            make

            cd src
            sudo make PREFIX=/opt/redis3 install
            sudo mkdir -p /opt/redis3/conf
            sudo cp $DEST/redis/redis-$REDIS_VERSION/redis.conf /opt/redis3/conf

            sudo ln -sf /opt/redis3/conf /etc/redis3
            sudo cp $DEST/redis/redis-$REDIS_VERSION/src/redis-trib.rb /opt/redis3/bin/

            sudo chown -hR $STACK_USER /opt/redis3/
            if is_ubuntu || is_fedora; then
                _configure_redis
            fi

            install_package -y ruby

            if ! sudo gem list redis | grep -q redis; then
                sudo gem source -a $DF_RUBY_SOURCE_ADD
                if [ -n "$DF_RUBY_SOURCE_REMOVE" ];then
                    sudo gem source -r $DF_RUBY_SOURCE_REMOVE
                fi
                sudo gem install redis
            fi

            popd
        fi
    else
        if is_ubuntu; then
           install_package -y redis-server
        elif is_fedora; then
           install_package -y redis
        fi
    fi
}

function nb_db_driver_install_client {
    sudo pip install "redis>=3.3.2"
    sudo pip install "hiredis>=1.0.0"
}

function nb_db_driver_start_server {
    _redis_env
    create=
    if is_service_enabled df-redis-server ; then
        if is_ubuntu || is_fedora; then
            # to acquire if should recreate cluster
            for redisserver in $REDIS_SERVER_LIST; do
                for port in $REDIS_PORT; do
                    test -f /opt/redis3/conf/nodes-$port.conf || { create=true; break 2 ; }
                done
            done
            # start redis
            for redisserver in $REDIS_SERVER_IPS; do
                for port in $REDIS_PORT; do
                    echo $redisserver:$port
                    pushd /opt/redis3/
                    [ "$create" ] && {

                        sudo rm nodes* -rf
                    }

                    ./bin/redis-server ./conf/redis-$port.conf &
                    redis_cluster="$redis_cluster"" ""$redisserver:$port"
                    popd
                done
            done
            # create cluster
            [ "$create" ] && {
                echo "Create the Redis cluster: "$redis_cluster
                pushd /opt/redis3/bin/
                echo "yes" | sudo ./redis-trib.rb create --replicas 1 $redis_cluster
                popd
            }

        fi
    fi
}

function nb_db_driver_stop_server {
    _redis_env
    if is_service_enabled df-redis-server ; then
        if is_ubuntu || is_fedora; then
            for redisserver in $REDIS_SERVER_IPS; do
                for port in $REDIS_PORT; do
                    echo "Shutdown Redis: "$redisserver:$port
                    sudo /opt/redis3/bin/redis-cli -p $port shutdown
                    pushd /opt/redis3/
                    sudo rm -rf nodes*.conf
                    sudo rm -rf dump*.rdb
                    sudo netstat -apn | grep $port | awk '{print $7}' | cut -d '/' -f1 | xargs sudo kill -9
                    popd
                done
            done
        fi
    fi
}

function nb_db_driver_clean {
    sudo rm -rf /opt/redis3
    if [ "$DF_REDIS_INSTALL_FROM_RUBY" == "True" ]; then
        sudo gem uninstall redis
    else
        if is_ubuntu; then
           uninstall_package -y redis-server
        elif is_fedora; then
           uninstall_package -y redis
        fi
    fi
}

function nb_db_driver_configure {
    :
}
